#include "stack.h"

 stack stack_init(void) {
	stack s = (stack)malloc(sizeof(struct cstk));
	s->cstksize = 0;
	s->entries = NULL;
	return(s);
 }

 void  stack_push(stack s, void* value, size_t size) {
	sentry* item = (sentry*)malloc(sizeof(sentry));
	item->data = (size == 0) ? (int*)value : memcpy(malloc(size),value,size);
	item->next = s->entries;
	s->entries = item;
	s->cstksize++;
	return;
 }

 void* stack_pop(stack s) {
	void* item = s->entries->data;
	s->entries = s->entries->next;
	s->cstksize--;
	return item; 
 }

 void  stack_destroy(stack s) {
	sentry* item = s->entries;
	sentry* temp;
	while(item != NULL) {
		temp = item;
		free(item->data);
		item = item->next;
		free(temp);
	}
	free(s);
 }

 void  stack_print(stack s) {
	sentry* item = s->entries;
	printf("[ ");
	while(item != NULL) {
		printf("%x ",(int)item->data);
		item = item->next;
	}
        printf("]\n");
 }

